<?php

namespace App\Jobs;
use App\Models\XnMyyGoodsInterconnection;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;


class SyncRecharge implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct(public $data,public $url = ''){
        $this -> url = env('BATCH_RECHARGE_URL');
    }


    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try {
            $data_res = [
                'order_sn'=>$this -> data['order_no'],
                'goods_id'=>$this -> data['gid'],
                'account'=>$this -> data['account'],
            ];
            ksort($data_res);
            Log::channel('job') -> info('请求数组：',$data_res);
            $data_res['sign'] = md5(json_encode($data_res).$this -> data['secret']);
            $res =  httpClient() -> asJson() -> post($this -> url,$data_res);
            Log::channel('job') -> info('下单结果：',[
                    'url' => $this->url,
                    'data' => $this->data,
                    'res' => $res,
                ]);
        }catch (\Exception $e){
            Log::channel('job') -> info('同步下单失败',[
                'url' => $this->url,
                'data' => $this->data,
                'message' => $e->getMessage(),
            ]);
        }

    }
}
